<!-- chat.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input id="message" type="text">
<button onclick="send()">发送</button>
<div id="messages"></div>

<script>
    const ws = new WebSocket("ws://localhost:9191/chat");

    ws.onopen = () => {
        console.log("连接成功");
    };

    ws.onmessage = (event) => {
        document.getElementById("messages").innerHTML += event.data + "<br>";
    };

    ws.onclose = () => {
        console.log("连接关闭");
    };

    ws.onerror = () => {
        console.log("连接出错");
    };

    function send() {
        const msg = document.getElementById("message").value;
        ws.send(msg);
        document.getElementById("message").value = "";
    }
</script>
</body>
</html>

